import pandas as pd
from matplotlib import pyplot as plt
import matplotlib as mlp
import numpy as np


class MyFigure:
    mlp.rcParams['font.sans-serif'] = ['SimHei']
    mlp.rcParams['axes.unicode_minus'] = False
    FIG_SIZE = [6, 5]

    def __init__(self, data):
        self.data = data

    def get_bar_on_areas(self):
        target_df = self.data.get_province_statistic()
        cities = list(target_df.index)[:5]
        counts = list(target_df)[:5]

        fig = plt.figure(figsize=self.FIG_SIZE)
        for a, b in zip(np.arange(len(cities)), counts):
            plt.text(a, b, '%.2f' % b, ha='center', va='bottom', fontsize=8)
        plt.bar(cities, counts, width=0.5)
        plt.title("各地区学校内岗位需求统计表", size=10)
        plt.xlabel("城市", size=8)
        plt.ylabel("学校岗位人才需求数量", size=8)
        plt.xticks(cities)
        return fig

    def get_bar_on_types(self):
        target_df = self.data.get_type_category()
        type_list = list(target_df.index)
        type_counts = list(target_df)
        fig = plt.figure(figsize=self.FIG_SIZE)
        plt.barh(type_list, type_counts, height=0.5)
        plt.title("各类发布招聘的学校数量统计", size=14)
        plt.ylabel("学校类型", size=12)
        plt.xlabel("学校数量", size=12)
        return fig

    def get_pie_on_teacher(self):
        target_df = self.data.get_teacher_category()
        fig = plt.figure(figsize=self.FIG_SIZE)
        plt.pie([len(target_df), len(self.data.get_data()) - len(target_df)], labels=['教师岗位', '其他岗位'],
                autopct='%.2f%%', startangle=90)
        plt.title("校内岗位中教师岗位的占比")
        plt.legend()
        return fig

    def get_pie_on_education(self):
        teacher_df = self.data.get_teacher_category()
        middle_school = teacher_df[(teacher_df['companyTypeName']) == '中小学']
        education_df = teacher_df['education'].value_counts()
        education_middle_df = middle_school['education'].value_counts()
        fig, axes = plt.subplots(1, 2)
        axes[0].pie(list(education_df), labels=list(education_df.index),
                    autopct='%.2f%%', startangle=180, explode=(0, 0, 0.08, 0, 0))
        axes[0].axis('equal')
        axes[0].set_title("所有教师岗位中最低学历占比")
        axes[0].legend()

        axes[1].pie(list(education_middle_df), labels=list(education_middle_df.index),
                    autopct='%.2f%%', startangle=180, explode=(0.08, 0, 0, 0, 0))
        axes[1].axis('equal')
        axes[1].set_title("中小学教师岗位中最低学历占比")
        axes[1].legend()
        plt.tight_layout()
        return fig

    def max_min_teacher_difference(self):
        combine_df = self.data.get_max_min_difference_teacher()
        fig, axes = plt.subplots(1, 2)
        i = 0
        for index, row in combine_df.iterrows():
            axes[i].pie([row['教师岗位数'], row['差值']], labels=['教师岗位', '其他岗位'], autopct='%.2f%%', startangle=45,
                        explode=(0.08, 0.05), colors=['red', 'lightskyblue'])
            axes[i].axis('equal')
            axes[i].set_title(row['热门专业'] + "专业教师岗位占比", size=14)
            axes[i].legend()
            i += 1
        plt.tight_layout()
        return fig

    def one_two_difference(self):
        one_and_two_df = self.data.get_one_and_two()
        province_index = list(one_and_two_df.index)
        difference_list = list(one_and_two_df['差值'])
        colors = ['r', 'b']
        fig = plt.figure(figsize=[6, 5])

        for i in range(0, len(province_index)):
            color = colors[difference_list[i] < 0]
            plt.bar(province_index[i], difference_list[i], color=color)

        plt.title("双一流院校招聘岗位数与普通院校招聘岗位数差值", size=14)
        plt.xlabel("province", size=12)
        plt.ylabel("差值", size=12)
        return fig

    def get_divided_in_shanghai(self):
        shanghai_result = self.data.get_divided_in_shanghai()
        shanghai_result.plot(kind='bar')

    def get_five_month_plot(self):
        month_dict = self.data.get_last_five_month()
        plt.plot(month_dict.values(), marker='*', linestyle='--', label='发布岗位数')
        plt.xticks(range(len(month_dict.keys())), month_dict.keys())
        plt.legend()
        plt.show()

